<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
    <script src="https://unpkg.com/vuex@next"></script>
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      // import {createApp} from 'vue'
      // import {createStore} from 'vuex'
      const { createApp } = Vue;
      const { createStore } = Vuex;

      const moduleA = {
        state: () => ({
          count: 100,
        }),
        mutations: {
          increment(state) {
            state.count += 100;
          },
        },
        getters: {
          doubleCount(state) {
            return state.count * 2;
          },
          sumWithRootCount(state, getters, rootState) {
            return state.count + rootState.count;
          },
        },
        actions: {
          incrementIfOddOnRootSum({ state, commit, rootState }) {
            console.log(
              "incrementIfOddOnRootSum",
              state.count,
              rootState.count
            );
            if ((state.count + rootState.count) % 2 === 1) {
              commit("increment");
            }
          },
        },
      };

      const store = createStore({
        state: {
          count: 0,
        },
        mutations: {
          increment(state) {
            state.count++;
          },
        },
        modules: {
          a: moduleA,
        },
      });

      const app = createApp({
        template: `
            <div>root: {{ $store.state.count }}</div>
            <div>a: {{ $store.state.a.count }}</div>
            <div>doubleCount: {{ $store.getters.doubleCount }}</div>
            <div>sumWithRootCount: {{ $store.getters.sumWithRootCount }}</div>
            <button @click="$store.commit('increment')">Add</button>
            <button @click="$store.dispatch('incrementIfOddOnRootSum')">incrementIfOddOnRootSum</button>
        `,
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>
